var radial = function (d, p, a) {

this.drawing = d;
this.parent = p;
this.children = [];

this.attr = {
 hubCenter: (a.hubCenter) ? a.hubCenter : {x: "0px", y: "0px"},
 hubRadius: (a.hubRadius) ? a.hubRadius : "20px",
 hubTitle: (a.hubTitle) ? a.hubTitle : "",
 hubSelected: (a.hubSelected) ? a.hubSelected : false,
 hubVisible: (a.hubVisible) ? a.hubVisible : true,
 fillColor: (a.fillColor) ? a.fillColor : "#999",
 lineColor: (a.lineColor) ? a.lineColor : "#333",
 lineStroke: (a.lineStroke) ? a.lineStroke : "5px",
 hoverColor: (a.hoverColor) ? a.hoverColor : "#000888",
 hoverStroke: (a.hoverStroke) ? a.hoverStroke : "5px",
 selectColor: (a.selectColor) ? a.selectColor : "#f00",
 selectStroke: (a.selectStroke) ? a.selectStroke : "5px",
 titleVisible: (a.titleVisible) ? a.titleVisible : true,
 titleColor: (a.titleColor) ? a.titleColor : "#fff",
 titleOffset: (a.titleOffset) ? a.titleOffset :  {x: "10px", y: "5px"},
 childrenVisible: (a.childrenVisible) ? a.childrenVisible : true,
 childrenRadius: (a.childrenRadius) ? a.childrenRadius : "120px",
 childrenRotateRadians: (a.childrenRotateRadians) ? a.childrenRotateRadians : Math.PI/2,
 childrenConnector: (a.childrenConnector) ? a.childrenConnector : "polygon"
}; 

var childConnectorTypes = ["none", "circle", "star", "spokes", "polygon"];

var anchors = [];
var hub = [];
var spokes = [];
var circles = [];
var starlines = [];
var polygons = [];


if (this.parent && this.parent.children) {
 this.parent.children.push(this);
} else {
}

this.click = function() {
 return false;
};

this.mousedown = function() {
 return false;
};

this.mouseup = function() {
 if (this.attr.childrenVisible) {
  this.hideChildren();
 } else {
  this.showChildren();
 }
 this.draw();
};

this.mouseover = function() {
 this.hoverHub();
 return false;
};

this.mouseout = function() {
 this.plainHub();
 return false;
};

this.selectHub = function() {
 if(hub[0]) {
  hub[0].set( {strokecolor: this.attr.selectColor, strokeweight: this.attr.selectStroke } );
 }
};

this.unselectHub = function() {
 if(hub[0]) {
  hub[0].set( {strokecolor: this.attr.lineColor, strokeweight: this.attr.lineStroke } );
 }
};

this.hoverHub = function() {
 if(hub[0]) {
  hub[0].set( {strokecolor: this.attr.hoverColor, strokeweight: this.attr.hoverStroke } );
 }
};

this.plainHub = function() {
 if(hub[0]) {
  hub[0].set( {strokecolor: this.attr.lineColor, strokeweight: this.attr.lineStroke } );
 }
};

this.showHub = function () {
 this.attr.hubVisible = true;
};

this.hideHub = function () {
 this.attr.hubVisible = false;
}

this.hideChildren = function () {
 this.attr.childrenVisible = false;
 for (var a=0; a<this.children.length; a++) {
  this.children[a].hideChildren();
  this.children[a].hideHub();
 }
};

this.showChildren = function() {
 this.attr.childrenVisible = true;
 for (var a=0; a<this.children.length; a++) {
  // this.children[a].showChildren();
  this.children[a].showHub();
 }
};

this.draw = function () {

 if (this.attr.childrenConnector == "polygon") { this.drawPoly(); }
 if (this.attr.childrenConnector == "star")    { this.drawStar(); }
 if (this.attr.childrenConnector == "spokes")  { this.drawSpokes(); }
 if (this.attr.childrenConnector == "circle")  { this.drawCircle(); }

 this.drawChildren();
 this.drawHub();

};

this.drawChildren = function () {
 var d = (this.children.length) ? ((2*Math.PI)/this.children.length) : 0;
 var cx = parseInt(this.attr.hubCenter.x);
 var cy = parseInt(this.attr.hubCenter.y);
 var r  = (this.attr.childrenVisible) ? parseInt(this.attr.childrenRadius) : parseInt(this.attr.hubRadius);
 var rr = parseFloat(this.attr.childrenRotateRadians); 
 
 for (var a=0; a<this.children.length; a++) {
  this.children[a].attr.hubCenter.x = Math.round(r * Math.cos(a*d+rr) + cx) + "px";
  this.children[a].attr.hubCenter.y = Math.round(r * Math.sin(a*d+rr) + cy) + "px";
  this.children[a].draw();
 }
};


this.drawHub = function() {

 var hubattr = {
  x: this.attr.hubCenter.x,
  y: this.attr.hubCenter.y,
  r: this.attr.hubRadius,
  strokecolor: this.attr.lineColor,
  strokewidth: this.attr.lineStroke,
  fillcolor: this.attr.fillColor
 };

 var tx = Math.round(
  parseInt(this.attr.hubCenter.x) + parseInt(this.attr.titleOffset.x) 
 ) + "px";
 var ty = Math.round(
  parseInt(this.attr.hubCenter.y) + parseInt(this.attr.titleOffset.y) 
 ) + "px";




 if(!hub[0]) {
  
  hub[0] = this.drawing.circle(this.drawing.drawing, hubattr);

  YAHOO.util.Event.addListener(hub[0], "mouseover", this.mouseover, this, true);
  YAHOO.util.Event.addListener(hub[0], "mouseout", this.mouseout, this, true);
  YAHOO.util.Event.addListener(hub[0], "mousedown", this.mousedown, this, true);
  YAHOO.util.Event.addListener(hub[0], "mouseup", this.mouseup, this, true);
  YAHOO.util.Event.addListener(hub[0], "click", this.click, this, true);

  hub[1] = document.createElement("span");
  hub[1].onmousedown = function() { return false; };
  hub[1].appendChild(document.createTextNode(this.attr.hubTitle));
  hub[1].style.position = "absolute";
  hub[1].style.backgroundColor = this.attr.lineColor;
  hub[1].style.color = this.attr.titleColor;
  // hub[1].style.border = this.attr.lineStroke + " Solid " + this.attr.titleColor;
  hub[1].style.left = tx;
  hub[1].style.top = ty;
  hub[1].className = "hubText";
  this.drawing.el.appendChild(hub[1]);			
 }

 hub[0].set(hubattr);	    
 
 hub[1].style.left = tx;
 hub[1].style.top = ty;
 
 if (this.attr.hubVisible) {
  hub[1].style.display = "inline";
 } else {
  hub[1].style.display = "none";		    
 }
 
};

this.drawSpokes = function() {

 var d = (this.children.length) ? ((2*Math.PI)/this.children.length) : 0;
 var cx = parseInt(this.attr.hubCenter.x);
 var cy = parseInt(this.attr.hubCenter.y);
 var r  = (this.attr.childrenVisible) ? parseInt(this.attr.childrenRadius) : parseInt(this.attr.hubRadius);
 var rr = parseFloat(this.attr.childrenRotateRadians); 
 for(var c=0; this.children.length > c; c++) {
  var spokeattr = {
    x1: this.attr.hubCenter.x,
    y1: this.attr.hubCenter.y,
    x2: Math.round( r * Math.cos(c*d+rr) + cx ) + "px",
    y2: Math.round( r * Math.sin(c*d+rr) + cy ) + "px",
    strokewidth: this.attr.lineStroke,
    strokecolor: this.attr.lineColor
   };
  if(!spokes[c]) {   
   spokes[c] = this.drawing.line(this.drawing.drawing, spokeattr);			
  } else {
   spokes[c].set(spokeattr);
  }
 }
};

this.drawCircle = function() {
 // var d = (this.children.length) ? ((2*Math.PI)/this.children.length) : 0;
 var cx = parseInt(this.attr.hubCenter.x);
 var cy = parseInt(this.attr.hubCenter.y);
 var r  = (this.attr.childrenVisible) ? parseInt(this.attr.childrenRadius) : parseInt(this.attr.hubRadius);
 var attr= {
  x: this.attr.hubCenter.x,
  y: this.attr.hubCenter.y,
  r: r,
  strokewidth: this.attr.lineStroke,
  strokecolor: this.attr.lineColor
 };
 if(!circles[0]) {   
  circles[0] = this.drawing.circle(this.drawing.drawing, attr);			
 } else {
  circles[0].set(attr);
 }
};

this.drawPoly = function() {
 var s = 2;
 var d = (this.children.length) ? ((2*Math.PI)/this.children.length) : 0;
 var cx = parseInt(this.attr.hubCenter.x);
 var cy = parseInt(this.attr.hubCenter.y);
 var r  = (this.attr.childrenVisible) ? parseInt(this.attr.childrenRadius) : parseInt(this.attr.hubRadius);
 var rr = parseFloat(this.attr.childrenRotateRadians); 
 for(var c=0; this.children.length > c; c++) {
  var spokeattr = {
    x1: Math.round( r * Math.cos(c*d+rr) + cx ) + "px",
    y1: Math.round( r * Math.sin(c*d+rr) + cy ) + "px",
    x2: Math.round( r * Math.cos((c+s)*d+rr) + cx ) + "px",
    y2: Math.round( r * Math.sin((c+s)*d+rr) + cy ) + "px",
    strokewidth: this.attr.lineStroke,
    strokecolor: this.attr.lineColor
   };
  if(!spokes[c]) {   
   spokes[c] = this.drawing.line(this.drawing.drawing, spokeattr);			
  } else {
   spokes[c].set(spokeattr);
  }
 }
};

};